﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using qlbansach_thuctapnhom.DAL;
using qlbansach_thuctapnhom.BLL;
namespace qlbansach_thuctapnhom.form
{
    public partial class formtacgia : Form
    {
        public formtacgia()
        {
            InitializeComponent();
            hienthi();
        }
        quanlybansachDataContext db = new quanlybansachDataContext();
        tacgia_bll tg = new tacgia_bll();
        //=======================================================
        public void hienthi()
        {
            db = new quanlybansachDataContext();
            var hienthi = from ht in db.Tacgias select ht;
            dgv.DataSource = hienthi;
            dgv.DataBindings.Clear();
        }
        //=======================================================
        private void btthem_Click(object sender, EventArgs e)
        {
            Tacgia obj = new Tacgia();
            bool ok = tg.kiemtratacgia(txtmatg.Text);
            if (ok)
            {

                obj.matg = txtmatg.Text;
                obj.hoten = txttentg.Text;
                tg.themtacgia(obj);
                lblerror.Text = "Thêm thành công";

            }
            else MessageBox.Show("Thêm thất bại","Lỗi",MessageBoxButtons.OK,MessageBoxIcon.Error);
            hienthi();
        }
        //=======================================================
        private void btnhaplai_Click(object sender, EventArgs e)
        {
            foreach (var c in groupPanel1.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Text = "";
            }
        }
        //=======================================================
        private void btthoat_Click(object sender, EventArgs e)
        {
            formchinh f = new formchinh();
            f.Show();
            this.Close();
        }
        //=======================================================
        private void btsua_Click(object sender, EventArgs e)
        {
            bool ok = tg.kiemtratacgia(txtmatg.Text);
            if (!ok)
            {
                tg.sua(txtmatg.Text,txttentg.Text);
              
                lblerror.Text = "Sửa thành công";
                hienthi();
            }
            else MessageBox.Show("Sửa thất bại","Lỗi",MessageBoxButtons.OK,MessageBoxIcon.Error);
            
        }
        //=======================================================
        private void btxoa_Click(object sender, EventArgs e)
        {
            bool ok = tg.kiemtratacgia(txtmatg.Text);
            if (!ok)
            {
                tg.xoa(txtmatg.Text);            
                  lblerror.Text = "Xóa thành công";
                  hienthi();
            
            }
            else MessageBox.Show("Không tìm thấy đối tượng cần xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            hienthi();
        }
        //=======================================================
        private void txtten_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int n = e.RowIndex;//n is the location of the selected line            
            txtmatg.Text = dgv.Rows[n].Cells[0].Value.ToString();
            txttentg.Text = dgv.Rows[n].Cells[1].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool ok = tg.timkiemmatacgia(txtmatg.Text);
            if(ok)
            {
                dgv.DataSource=from tgia in db.Tacgias where tgia.matg.Equals(txtmatg.Text) select tgia;
                dgv.DataBindings.Clear();
                lblerror.Text="Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
               
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bool ok = tg.timkietentacgia(txttentg.Text);
            if (ok)
            {
                dgv.DataSource = from tgia in db.Tacgias where tgia.hoten.Equals(txttentg.Text) select tgia;
                dgv.DataBindings.Clear();
                lblerror.Text = "Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        //=======================================================
    }
}
